package com.itycu.server.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @description:
 * @author: zhoucheng
 * @create: 2022-05-03 12:24
 **/
public class CodeUtil {
    /**
     * 单号
     *
     * @return
     */
    public static String generateSN(String ctype, Long userid) {
        StringBuffer sNBuffer = new StringBuffer();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        sNBuffer.append(ctype);
        sNBuffer.append(format.format(new Date()));
        sNBuffer.append(getRandom(4));
        sNBuffer.append(userid);
        return sNBuffer.toString();
    }

    /**
     * 生成指定位数的随机数
     * @param length
     * @return
     */
    public static String getRandom(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            val += String.valueOf(random.nextInt(10));
        }
        return val;
    }
}
